<?php
/**
 * Created by PhpStorm.
 * User: zhou
 * Date: 2019/3/17
 * Time: 下午11:58
 */

namespace App\Models;


use Illuminate\Database\Eloquent\Model;

class HotelOwnerApply extends Model
{
    protected $table = 'hotel_owners_apply';
    protected $fillable = [
        'id','hotel_id','open_id','phone','status','updated_at','created_at',
    ];

    public static $error;//error存放处

    private static $hotel_owner_model_tmp;

    /**
     * 商家入驻申请列表
     */
    public function apply_list($search_arr)
    {
        $query = self::query()
            -> select('id','hotel.name',$this->table.'.phone',$this->table.'.updated_at',$this->table.'.status')
            -> leftJoin('hotel','hotel.hotel_id','=',$this->table.'.hotel_id');
        $search_arr['search_'.$this->table.'.phone'] = $search_arr['search_phone']??'';
        unset($search_arr['search_phone']);
        $search_arr[$this->table.'.hotel_id'] = $search_arr['hotel_id']??-1;
        unset($search_arr['hotel_id']);
        $query = handle_search($query,$search_arr);
        $list = $query -> paginate(10);
        $pages = $list -> render() -> toHtml();
        return [
          'pages' => $pages,
          'data'  => $list -> toArray(),
        ];
    }

    public function update_status_byid($postdata)
    {
        $model = self::query()->find($postdata['id']);
        if(empty($model)){
            return true;
        }
        if(empty($postdata['status'])){
            return true;
        }
        $model -> status = $postdata['status'];
        return $model -> save();
    }

    /**检查该用户已入驻酒店
     * @param $hotel_open_id
     */
    public static function is_valid_hotel_owner($hotel_open_id)
    {
       $res =  self::query()
            -> where('open_id','=',$hotel_open_id)
            -> where('status','=',1)
            -> first() ;
       if($res){
           $res =  HotelOwner::query()
               -> from('hotel_owners')
               -> where('open_id','=',$hotel_open_id)
               -> first();
           if(empty($res)){
               return false;
           }
           return $res;
       }
       return false;

    }

    public static function get_hotel_apply_status($post)
    {
        if(isset($post['hotel_open_id'])){
            $post['open_id'] = empty($post['open_id']) ? $post['hotel_open_id']:$post['open_id'] ;
        }
        unset($post['hotel_open_id'],$post['true_name'],$post['phone']);
        $query = self::query();
        foreach($post as $filed => $v){
            $query = $query -> where($filed,'=',$v);
        }
        $hotel_owner_apply_model = $query -> first();
        if(empty($hotel_owner_apply_model)){
            return false;
        }
        self::$hotel_owner_model_tmp = $hotel_owner_apply_model;
        return $hotel_owner_apply_model -> status;
    }

    /**商家申请入驻酒店
     * @param $post
     * @return bool
     */
    public static function do_apply($post)
    {
        $hotel_status_with_open_id = self::get_hotel_apply_status($post);
        $return = true;
        //该Open_id的用户从未入驻该酒店
        if($hotel_status_with_open_id === false){
            $hotel_status_without_open_id = self::get_hotel_apply_status([
                'hotel_id' => $post['hotel_id']
            ]);

            //该酒店从未入驻过open_id
            if($hotel_status_without_open_id === false){
                //插入记录
                self::query()
                    -> insert([
                        'open_id'  => $post['hotel_open_id'],
                        'phone'    => $post['phone'],
                        'hotel_id' => $post['hotel_id'],
                    ]);
                return true;
            }
            switch ($hotel_status_without_open_id){
                case 0 :
                    self::$error = '该酒店已经有其他商家入驻中';
                    $return = false;
                    break;
                case 1:
                    self::$error = '该酒店已被其他商家入驻';
                    $return = false;
                    break;
                case 2:
                    //可以入驻，插入记录
                    self::query()
                        -> insert([
                            'open_id'  => $post['hotel_open_id'],
                            'phone'    => $post['phone'],
                            'hotel_id' => $post['hotel_id'],
                        ]);
                    break;
                case 3:
                    break;
            }
           return $return;
        }

        switch ($hotel_status_with_open_id){
            case 0 :
              self::$error = '您已提交申请，请等待管理员处理!';
              $return = false;
            break;
            case 1:
              self::$error = '您已入驻该酒店!';
                $return = false;
            break;
            case 2:
              //重新申请，更改申请状态为1
              self::$hotel_owner_model_tmp -> status = 0;
              self::$hotel_owner_model_tmp -> save();
            break;
        }
        return $return;
    }







}